﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.PluginKernel;
using Spc;

namespace EasyB2C
{
	
	/// <summary>
	/// 使用/test.sh/开始访问
	/// </summary>
    [PluginInfo("插件DEMO", "官方", "", "")]
    public class DemoExtends:IPlugin
    {
    	/// <summary>
    	/// 插件访问模块名称（使用/test.sh/访问）
    	/// </summary>
    	const string moduleName="test";
    	
    	/// <summary>
    	/// 初始化插件
    	/// </summary>
    	/// <param name="_app"></param>
    	/// <returns></returns>
        public PluginConnectionResult Connect(IPluginApp _app)
        {
            ExtendsPlugin app = _app as ExtendsPlugin;
            if (app != null)
            {
            	//注册
            	app.OnExtendModuleRequest+= app_OnExtendModuleRequest;
            	app.OnExtendModulePost+= app_OnExtendModulePost;
            }
            return PluginConnectionResult.Success;
        }
        
        /// <summary>
        /// 检查是否访问当前插件
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        bool CheckSource(string module)
        {
        	return String.Compare(module,moduleName,true)==0;
        }
        

        void app_OnExtendModuleRequest(CmsContext t, string module, string segments, ref bool handled)
        {
        	if(!this.CheckSource(module)) return;
        	
    		string[] parameters=segments.Split('/');
    		if(parameters[0]=="form")
    		{
    		    t.Response.Write("<form action='/test.sh/post' method='post' target='_blank'>"+
    			                 "姓名：<input type='text' value='John' name='name'/><br />"+
								 "年龄: <input type='text' value='18' name='age'/><br />"+
								 "<input type='submit' value='提交'/>"+
								 "</form>");
    			                 
    		}
    		else
    		{
    			t.Response.Write(String.Format("access info:<br />Module:{0}&nbsp;Action:{1}",module,parameters[0]));
    			
    			t.Response.Write("<a href='/test.sh/form'>Form Demo</a>");
    		}
    		
    		handled=true;	
        	
        }
        void app_OnExtendModulePost(CmsContext t, string module, string segments, ref bool handled)
        {
        	if(!this.CheckSource(module)) return;
        	
    		if(segments.Split('/')[0]=="post")
    		{
    			foreach(string key in t.Request.Form.Keys)
    			{
    				t.Response.Write(String.Format("key:{0},value:{1}<br />",key,t.Request.Form[key]));
    			}
    			
    			//标记为已经处理
    			handled=true;
    		}
        }

        
        #region IPlugin的成员
        
        public string GetMessage()
        {
            return "";
        }

        public bool Install()
        {
            return true;
        }

        public void Pause()
        {
        }

        public void Run()
        {
        }

        public bool Uninstall()
        {
            return false;
        }

        #endregion



    }
}
